{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  刘婷婷\n",
    "+ 学号  0154590"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "当我和世界不一样\n",
      "那就让我不一样\n",
      "坚持对我来说就是以柔克刚\n",
      "\n",
      "我如果对自己妥协\n",
      "如果对自己说谎\n",
      "即使别人原谅\n",
      "我也不能原谅\n",
      "\n",
      "\n",
      "最美的愿望一定最疯狂\n",
      "我就是我自己的神\n",
      "在我活的地方\n",
      "\n",
      "我和我骄傲的倔强\n",
      "握紧双手绝对不放\n",
      "下一站不是天堂\n",
      "就算失望不能绝望\n",
      "我和我骄傲的倔强\n",
      "我在风中大声地唱\n",
      "这一次为自己疯狂\n",
      "就这一次 我和我的倔强\n",
      "\n",
      "对 爱我的人别紧张\n",
      "我的固执很善良\n",
      "我的手越肮脏\n",
      "眼神越是发光\n",
      "\n",
      "你 不在乎我的过往\n",
      "看到了我的翅膀\n",
      "你说被火烧过\n",
      "才能出现凤凰\n",
      "逆风的方向\n",
      "更适合飞翔\n",
      "我不怕千万人阻挡\n",
      "只怕自己投降\n",
      "\n",
      "我和我骄傲的倔强\n",
      "握紧双手绝对不放\n",
      "下一站不是天堂\n",
      "就算失望不能绝望\n",
      "我和我骄傲的倔强\n",
      "我在风中大声地唱\n",
      "这一次为自己疯狂\n",
      "就这一次 我和我的倔强\n",
      "\n",
      "我和我骄傲的倔强\n",
      "握紧双手绝对不放\n",
      "下一站不是天堂\n",
      "就算失望不能绝望\n",
      "我和我骄傲的倔强\n",
      "我在风中大声地唱\n",
      "这一次为自己疯狂\n",
      "就这一次 我和我的倔强\n"
     ]
    }
   ],
   "source": [
    "def repeat():\n",
    "    print(\"\\n我和我骄傲的倔强\\n握紧双手绝对不放\\n下一站不是天堂\\n就算失望不能绝望\\n我和我骄傲的倔强\\n我在风中大声地唱\\n这一次为自己疯狂\\n就这一次 我和我的倔强\") \n",
    "print(\"\\n当我和世界不一样\\n那就让我不一样\\n坚持对我来说就是以柔克刚\")\n",
    "print(\"\\n我如果对自己妥协\\n如果对自己说谎\\n即使别人原谅\\n我也不能原谅\\n\")\n",
    "print(\"\\n最美的愿望一定最疯狂\\n我就是我自己的神\\n在我活的地方\")\n",
    "repeat()\n",
    "print(\"\\n对 爱我的人别紧张\\n我的固执很善良\\n我的手越肮脏\\n眼神越是发光\")\n",
    "print(\"\\n你 不在乎我的过往\\n看到了我的翅膀\\n你说被火烧过\\n才能出现凤凰\\n逆风的方向\\n更适合飞翔\\n我不怕千万人阻挡\\n只怕自己投降\")\n",
    "repeat()\n",
    "repeat()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please enter the data you want to process, separated by Spaces.56 23 15 44 23 98 66 34 51 25 30 24\n",
      "Number's count is: 12\n",
      "Average value of parameter is: 40.75\n",
      "The variance is: 520.5208333333333\n"
     ]
    }
   ],
   "source": [
    "# 参数处理\n",
    "from pip._vendor.distlib.compat import raw_input\n",
    "\n",
    "def handlerParam(args):\n",
    "    print(\"Number's count is:\", len(args))\n",
    "    # 参数读取\n",
    "    numbers = []\n",
    "    for value in args:\n",
    "        numbers.append(value)\n",
    "        \n",
    "    print(\"Average value of parameter is:\", getAverage(numbers))\n",
    "    print(\"The variance is:\", getVariance(numbers))\n",
    "    \n",
    "# 平均值   \n",
    "def getAverage(args):\n",
    "    count = 0\n",
    "    for number in args:\n",
    "        count += number\n",
    "        \n",
    "    return count / len(args)\n",
    "\n",
    "# 方差\n",
    "def getVariance(args):\n",
    "    average = getAverage(args)\n",
    "    \n",
    "    variance = 0   \n",
    "    for value in args:\n",
    "        variance += (pow((value - average), 2)) / len(args)\n",
    "    return variance\n",
    "\n",
    "stringData = raw_input(\"Please enter the data you want to process, separated by Spaces.\")\n",
    "data = stringData.split()\n",
    "\n",
    "# 数据类型转换\n",
    "numbers = []\n",
    "for value in data:\n",
    "     numbers.append(int(value))\n",
    "# 数据处理        \n",
    "handlerParam(numbers)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please enter the data you want to process, separated by Spaces.2 2 3 4 4 5 5 6 6 7\n",
      "Please input your choice, V: output variance, D: output standard deviation.D\n",
      "Number's count is: 10\n",
      "Average value of parameter is: 4.4\n",
      "The deviation is: 1.624807680927192\n"
     ]
    }
   ],
   "source": [
    "# 参数处理\n",
    "from math import sqrt\n",
    "\n",
    "from pip._vendor.distlib.compat import raw_input\n",
    "\n",
    "def handlerParam(args, arg):\n",
    "    print(\"Number's count is:\", len(args))\n",
    "    # 参数读取\n",
    "    numbers = []\n",
    "    for value in args:\n",
    "        numbers.append(value)\n",
    "        \n",
    "    print(\"Average value of parameter is:\", getAverage(numbers))\n",
    "    if arg == \"V\" or arg == \"v\":\n",
    "        print(\"The variance is:\", getVariance(numbers))\n",
    "        \n",
    "    elif arg == \"D\" or arg == \"d\":\n",
    "        print(\"The deviation is:\", getDeviation(numbers))\n",
    "        \n",
    "    elif arg == \"\":\n",
    "         return\n",
    "    \n",
    "# 平均值   \n",
    "def getAverage(args):\n",
    "    count = 0\n",
    "    for number in args:\n",
    "        count += number\n",
    "        \n",
    "    return count / len(args)\n",
    "\n",
    "# 方差\n",
    "def getVariance(args):\n",
    "    average = getAverage(args)\n",
    "    \n",
    "    variance = 0   \n",
    "    for value in args:\n",
    "        variance += (pow((value - average), 2)) / len(args)\n",
    "    return variance\n",
    "\n",
    "# 标准差\n",
    "def getDeviation(args):\n",
    "    variance = getVariance(args)\n",
    "    \n",
    "    return sqrt(variance)\n",
    "\n",
    "stringData = raw_input(\"Please enter the data you want to process, separated by Spaces.\")\n",
    "choose = raw_input(\"Please input your choice, V: output variance, D: output standard deviation.\")\n",
    "data = stringData.split()\n",
    "\n",
    "# 数据类型转换\n",
    "numbers = []\n",
    "for value in data:\n",
    "     numbers.append(int(value))\n",
    "# 数据处理        \n",
    "handlerParam(numbers, choose)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please enter the data you want to process, separated by Spaces.2 3 4 4 5 6 7 7 8 9\n",
      "Please input your choice, V: output variance, D: output standard deviation.D\n",
      "Whether to obtain standard error, Y/y: yes, N/n: no.Y\n",
      "Whether to obtain the maxValue, Y/y: yes, N/n: no.Y\n",
      "Whether to obtain the minValue, Y/y: yes, N/n: no.Y\n",
      "Number's count is: 10\n",
      "Average value of parameter is: 5.5\n",
      "The deviation is: 2.1563858652847827\n",
      "The standardError is: 0.6819090848492928\n",
      "The maxValue is: 9\n",
      "The minValue is: 2\n"
     ]
    }
   ],
   "source": [
    "# 参数处理\n",
    "from math import sqrt\n",
    "\n",
    "from pip._vendor.distlib.compat import raw_input\n",
    "# args：数据字符串 arg：选择输出标准差/方差\n",
    "def handlerParam(args, arg, isGetStandardError, isGetMaxValue, isGetMinValue):\n",
    "    print(\"Number's count is:\", len(args))\n",
    "    \n",
    "    # 参数读取\n",
    "    numbers = []\n",
    "    for value in args:\n",
    "        numbers.append(value)\n",
    "            \n",
    "    print(\"Average value of parameter is:\", getAverage(numbers))\n",
    "    \n",
    "    if arg == \"V\" or arg == \"v\":\n",
    "        print(\"The variance is:\", getVariance(numbers))\n",
    "        \n",
    "    elif arg == \"D\" or arg == \"d\":\n",
    "         print(\"The deviation is:\", getDeviation(numbers))\n",
    "            \n",
    "    elif arg == \"\":\n",
    "         return\n",
    "   \n",
    "  # 标准误处理\n",
    "    if isGetStandardError == \"Y\" or isGetStandardError == \"y\":\n",
    "         print(\"The standardError is:\", getStandardError(numbers))\n",
    "    elif isGetStandardError == \"N\" or isGetStandardError == \"n\" or isGetStandardError == \"\":\n",
    "        return\n",
    "    \n",
    "    # 最大值处理\n",
    "    if isGetMaxValue == \"Y\" or isGetMaxValue == \"y\":\n",
    "        print(\"The maxValue is:\", max(numbers))\n",
    "    elif isGetMaxValue == \"N\" or isGetMaxValue == \"n\" or isGetMaxValue == \"\":\n",
    "         return\n",
    "    \n",
    "    # 最小值处理\n",
    "    if isGetMinValue == \"Y\" or isGetMinValue == \"y\":\n",
    "        print(\"The minValue is:\", min(numbers))\n",
    "    elif isGetMinValue == \"N\" or isGetMinValue == \"n\" or isGetMinValue == \"\":\n",
    "        return\n",
    "    \n",
    "# 平均值   \n",
    "def getAverage(args):\n",
    "    count = 0\n",
    "    for number in args:\n",
    "        count += number\n",
    "        \n",
    "    return count / len(args)\n",
    "\n",
    "# 方差\n",
    "def getVariance(args):\n",
    "    average = getAverage(args)\n",
    "    \n",
    "    variance = 0   \n",
    "    for value in args:\n",
    "        variance += (pow((value - average), 2)) / len(args)\n",
    "    return variance\n",
    "\n",
    "# 标准差\n",
    "def getDeviation(args):\n",
    "    variance = getVariance(args)\n",
    "    \n",
    "    return sqrt(variance)\n",
    "\n",
    "# 标准误\n",
    "def getStandardError(args):\n",
    "    return getDeviation(args) / sqrt(len(args))\n",
    "\n",
    "# 键盘录入数据\n",
    "stringData = raw_input(\"Please enter the data you want to process, separated by Spaces.\")\n",
    "choose = raw_input(\"Please input your choice, V: output variance, D: output standard deviation.\")\n",
    "isGetStandardError = raw_input(\"Whether to obtain standard error, Y/y: yes, N/n: no.\")\n",
    "isGetMaxValue = raw_input(\"Whether to obtain the maxValue, Y/y: yes, N/n: no.\")\n",
    "isGetMinValue = raw_input(\"Whether to obtain the minValue, Y/y: yes, N/n: no.\")\n",
    "# 数据分隔\n",
    "data = stringData.split()\n",
    "\n",
    "# 数据类型转换\n",
    "numbers = []\n",
    "for value in data:\n",
    "     numbers.append(int(value))\n",
    "# 数据处理        \n",
    "handlerParam(numbers, choose, isGetStandardError, isGetMaxValue, isGetMinValue)\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
